﻿using System.Diagnostics;
using System.Windows;
using LFSRecord2.ViewModel;

namespace LFSRecord2.Model.UserActions
{
    public class UserActionProjectSettingsChanged : UserActionBase
    {
        public string OldProjectName { get; set; }
        public string NewProjectName { get; set; }
        public string OldProjectFilePath { get; set; }
        public string NewProjectFilePath { get; set; }
        public double OldProjectLength { get; set; }
        public double NewProjectLength { get; set; }

        public byte OldFPS { get; set; }
        public byte NewFPS { get; set; }

        public string OldInSimIP { get; set; }
        public string NewInSimIP { get; set; }
        public ushort OldInSimPort { get; set; }
        public ushort NewInSimPort { get; set; }
        public string OldInSimAdminPassword { get; set; }
        public string NewInSimAdminPassword { get; set; }

        public bool OldRecordFullScreenOverride { get; set; }
        public bool NewRecordFullScreenOverride { get; set; }
        public byte OldRecordStopHotkey { get; set; }
        public byte NewRecordStopHotkey { get; set; }
        public ushort OldRecordingWidth { get; set; }
        public ushort NewRecordingWidth { get; set; }
        public ushort OldRecordingHeight { get; set; }
        public ushort NewRecordingHeight { get; set; }
        public bool OldRecordHide2D { get; set; }
        public bool NewRecordHide2D { get; set; }

        public bool OldMove3DAxesInvert { get; set; }
        public bool NewMove3DAxesInvert { get; set; }

        public int OldAudioOutputDevice { get; set; }
        public int NewAudioOutputDevice { get; set; }
        public uint OldSampleRate { get; set; }
        public uint NewSampleRate { get; set; }

        public UserActionProjectSettingsChanged()
        {
            Action = UserActionTypes.ProjectSettingsChanged;
        }

        public override void DoUndo()
        {
            Debug.WriteLine("Undoing a ProjectSettingsChanged action");
            MainWindowViewModel mwvm = (MainWindowViewModel)Application.Current.MainWindow.DataContext;
            if (NewProjectName != OldProjectName)
            {
                LfsRecordController.project.ProjectName = OldProjectName;
                mwvm.UpdateWindowTitle(LfsRecordController.project.ProjectName, LfsRecordController.project.ProjectFilePath);
            }
            if (NewProjectFilePath != OldProjectFilePath)
            {
                LfsRecordController.project.ProjectFilePath = OldProjectFilePath;
            }
            if (NewProjectLength != OldProjectLength)
            {
                LfsRecordController.project.ProjectLength = OldProjectLength;
                ((TimelineViewModel)mwvm.TimelineViewModels[0]).ProjectLength = LfsRecordController.project.ProjectLength;
            }
            if (NewFPS != OldFPS)
            {
                LfsRecordController.project.FPSChangeStretch(LfsRecordController.project.FPS, OldFPS);
                LfsRecordController.project.FPS = OldFPS;
            }
            if (NewInSimIP != OldInSimIP)
            {
                LfsRecordController.project.InSimIP = OldInSimIP;
            }
            if (NewInSimPort != OldInSimPort)
            {
                LfsRecordController.project.InSimPort = OldInSimPort;
            }
            if (NewInSimAdminPassword != OldInSimAdminPassword)
            {
                LfsRecordController.project.InSimAdminPassword = OldInSimAdminPassword;
            }
            if (NewRecordFullScreenOverride != OldRecordFullScreenOverride)
            {
                LfsRecordController.project.RecordFullScreenOverride = OldRecordFullScreenOverride;
            }
            if (NewRecordStopHotkey != OldRecordStopHotkey)
            {
                LfsRecordController.project.RecordStopHotkey = OldRecordStopHotkey;
            }
            if (NewRecordingWidth != OldRecordingWidth)
            {
                LfsRecordController.project.RecordingWidth = OldRecordingWidth;
            }
            if (NewRecordingHeight != OldRecordingHeight)
            {
                LfsRecordController.project.RecordingHeight = OldRecordingHeight;
            }
            if (NewRecordHide2D != OldRecordHide2D)
            {
                LfsRecordController.project.RecordHide2D = OldRecordHide2D;
            }
            if (NewAudioOutputDevice != OldAudioOutputDevice ||
                NewSampleRate != OldSampleRate)
            {
                if (LfsRecordController.PlaybackState != PlaybackStates.Stopped)
                    LfsRecordController.StopPlayback();
                LfsRecordController.Mixer.ResetIO(OldAudioOutputDevice, 2, 16, OldSampleRate);
            }
        }

        public override void DoRedo()
        {
            Debug.WriteLine("Redoing a ProjectSettingsChanged action");
            MainWindowViewModel mwvm = (MainWindowViewModel)Application.Current.MainWindow.DataContext;
            if (NewProjectName != OldProjectName)
            {
                LfsRecordController.project.ProjectName = NewProjectName;
                mwvm.UpdateWindowTitle(LfsRecordController.project.ProjectName, LfsRecordController.project.ProjectFilePath);
            }
            if (NewProjectFilePath != OldProjectFilePath)
            {
                LfsRecordController.project.ProjectFilePath = NewProjectFilePath;
            }
            if (NewProjectLength != OldProjectLength)
            {
                LfsRecordController.project.ProjectLength = NewProjectLength;
                ((TimelineViewModel)mwvm.TimelineViewModels[0]).ProjectLength = LfsRecordController.project.ProjectLength;
            }
            if (NewFPS != OldFPS)
            {
                LfsRecordController.project.FPSChangeStretch(LfsRecordController.project.FPS, NewFPS);
                LfsRecordController.project.FPS = NewFPS;
            }
            if (NewInSimIP != OldInSimIP)
            {
                LfsRecordController.project.InSimIP = NewInSimIP;
            }
            if (NewInSimPort != OldInSimPort)
            {
                LfsRecordController.project.InSimPort = NewInSimPort;
            }
            if (NewInSimAdminPassword != OldInSimAdminPassword)
            {
                LfsRecordController.project.InSimAdminPassword = NewInSimAdminPassword;
            }
            if (NewRecordFullScreenOverride != OldRecordFullScreenOverride)
            {
                LfsRecordController.project.RecordFullScreenOverride = NewRecordFullScreenOverride;
            }
            if (NewRecordStopHotkey != OldRecordStopHotkey)
            {
                LfsRecordController.project.RecordStopHotkey = NewRecordStopHotkey;
            }
            if (NewRecordingWidth != OldRecordingWidth)
            {
                LfsRecordController.project.RecordingWidth = NewRecordingWidth;
            }
            if (NewRecordingHeight != OldRecordingHeight)
            {
                LfsRecordController.project.RecordingHeight = NewRecordingHeight;
            }
            if (NewRecordHide2D != OldRecordHide2D)
            {
                LfsRecordController.project.RecordHide2D = NewRecordHide2D;
            }
            if (NewAudioOutputDevice != OldAudioOutputDevice ||
                NewSampleRate != OldSampleRate)
            {
                if (LfsRecordController.PlaybackState != PlaybackStates.Stopped)
                    LfsRecordController.StopPlayback();
                LfsRecordController.Mixer.ResetIO(NewAudioOutputDevice, 2, 16, NewSampleRate);
            }
        }
    }
}
